package cn.fl.continfo.controller;

import cmm.mid.core.framework.constant.tripartite.InterfaceConstants;
import cmm.mid.core.framework.dto.MessageDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import pam.yxfl.facade.api.IYxflApiService;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;

/**
 * @Description: 资方合同回传及重签控制层
 * @Author sunqian
 * @Date 2020/09/11
 */
@RestController
@RequestMapping(value = "/asset/cont/resign")
public class AssetContResignController extends BaseController {

    @Autowired
    IPrjBscInfoService prjBscInfoService;

    @Autowired
    IYxflApiService yxflApiService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toAssetContResignListPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toListPage(HttpServletRequest request) {
        BaseLogUtils.newLogger("合同配置加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName()).info();
        return new ModelAndView("continfo/assetcontresign/assetcontresignList");
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object initList(HttpServletRequest request) {
        BaseLogUtils.newLogger("合同配置初始化列表页面请求参数：" + this.getRequestParameter(request) + CurrentThreadContext.getCurrentUserName()).info();
        String pageNum = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String cstNm = request.getParameter("cstNm");
        String lsCntNo = request.getParameter("lsCntNo");
        Pager page = new Pager();
        PrjBscInfoQueryCondition prjBscInfoQueryCondition = new PrjBscInfoQueryCondition();
        if (StringUtils.isNotEmpty(pageNum)) {
            page.setCurrentPage(Integer.parseInt(pageNum));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        if (StringUtils.isNotEmpty(cstNm)) {
            prjBscInfoQueryCondition.setCstNm(cstNm);
        }
        if (StringUtils.isNotEmpty(lsCntNo)) {
            prjBscInfoQueryCondition.setLsCntNo(lsCntNo);
        }
        prjBscInfoQueryCondition.setInsCode(PrjBscInfoConstant.PRJ_INS_CODE_YXFL);
        prjBscInfoQueryCondition.setUsrId(CurrentThreadContext.getCurrentUserId());
        prjBscInfoQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        page.setCondition(prjBscInfoQueryCondition);
        BaseLogUtils.newLogger("合同配置列表页面返回结果page=" + JSON.toJSONString(page)).info();
        Pager pager = this.prjBscInfoService.searchListPage(page);
        List<PrjBscInfoVO> list = (List<PrjBscInfoVO>) pager.getResultList();
        for (PrjBscInfoVO prjBscInfoVO : list) {
            prjBscInfoVO.setAssetContStsCd("UNAUDITED");
            prjBscInfoVO.setAssetContStsNm("待审核");
        }
        list.sort(new Comparator<PrjBscInfoVO>() {
            @Override
            public int compare(PrjBscInfoVO o1, PrjBscInfoVO o2) {
                return o2.getId().compareTo(o1.getId());
            }
        });
        ListPageVO<PrjBscInfoVO> listPage = new ListPageVO<>(list, pager.getTotalCount());
        BaseLogUtils.newLogger("合同配置列表页面返回结果pager=" + JSON.toJSONString(listPage) + CurrentThreadContext.getCurrentUserName()).info();
        return listPage;
    }

    /**
     * 资方合同签署状态查询
     */
    @RequestMapping(value = "/selectAssetContStatus.do", method = {RequestMethod.GET, RequestMethod.POST})
    public DataResultVO selectAssetContStatus(HttpServletRequest request) {
        String prjId = request.getParameter("prjId");
        MessageDTO<InterfaceConstants.AuditResult> messageDTO = yxflApiService.queryContract(Long.parseLong(prjId));
        BaseLogUtils.newLogger("越秀合同状态查询结果prjId=" + prjId + ",messageDTO=" + JSON.toJSONString(messageDTO)).info();
        final boolean fail = null == messageDTO || !messageDTO.isSuccess() || null == messageDTO.getContent();
        if (fail) {
            return new DataResultVO(0, "查询合同接口失败");
        }
        String assetContSts = messageDTO.getContent();
        return new DataResultVO(1, "查询合同状态成功", assetContSts);
    }

    /**
     * 资方合同重签推送
     */
    @RequestMapping(value = "/resignPushCont.do", method = {RequestMethod.GET, RequestMethod.POST})
    public DataResultVO resignPushCont(HttpServletRequest request) {
        String prjId = request.getParameter("prjId");
        MessageDTO<InterfaceConstants.AuditResult> messageDTO = yxflApiService.queryContract(Long.parseLong(prjId));
        BaseLogUtils.newLogger("越秀合同查询结果prjId=" + prjId + ",messageDTO=" + JSON.toJSONString(messageDTO)).info();
        final boolean fail = null == messageDTO || !messageDTO.isSuccess() || null == messageDTO.getContent();
        if (fail) {
            return new DataResultVO(0, "重签推送获得合同结果失败");
        }
        String assetContSts = messageDTO.getContent();
        if ("CREATED".equals(assetContSts)) {
            PrjBscInfoVO prjBscInfoVO = prjBscInfoService.selectById(Long.parseLong(prjId));
            prjBscInfoVO.setOrgSignStsCd("3");
            boolean modify = prjBscInfoService.modify(prjBscInfoVO);
            if (modify) {
                return new DataResultVO(1, "重签推送成功");
            }
        }
        return new DataResultVO(0, "无需重签推送");
    }

    /**
     * 资方合同下载
     */
    @RequestMapping(value = "/downloadCont.do", method = {RequestMethod.GET, RequestMethod.POST})
    public DataResultVO downloadCont(HttpServletRequest request) {
        String prjId = request.getParameter("prjId");
        MessageDTO<InterfaceConstants.AuditResult> messageDTO = yxflApiService.downLoanContractByPrjId(Long.parseLong(prjId));
        BaseLogUtils.newLogger("越秀合同查询结果prjId=" + prjId + ",auditResultMessageDTO=" + JSON.toJSONString(messageDTO)).info();
        final boolean fail = null == messageDTO || !messageDTO.isSuccess() || null == messageDTO.getData() || !InterfaceConstants.AuditResult.pass.key().equals(messageDTO.getData().key());
        if (fail) {
            return new DataResultVO(0, "下载资方合同包失败");
        }
        return new DataResultVO(1, "下载成功");
    }

}
